﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ATSBLL;
using System.Text;
using System.IO;

public partial class TestDataReport_PIMReport_PIMReportExport : System.Web.UI.Page
{
    private Report_PIMData Ptest;

    public TestDataReport_PIMReport_PIMReportExport()
    {
        Ptest = new Report_PIMData();
    }
    public override void VerifyRenderingInServerForm(Control control)
    { 
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
     

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void BindTests()
    {
        if (!string.IsNullOrEmpty(txtProductSN.Text.Trim()))//!string.IsNullOrEmpty(tbStart.Value.ToString()) && !string.IsNullOrEmpty(tbEnd.Value.ToString())
        {
            //DateTime dtStart = DateTime.Parse(tbStart.Value.Trim());
            //DateTime dtEnd = DateTime.Parse(tbEnd.Value.Trim());
            string ProductSN = txtProductSN.Text.Trim();
            GridView1.DataSource = Ptest.GetProductSN(ProductSN);
            GridView1.DataBind();
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if (GridView1.Rows[i].Cells[8].Text == "Fail")
                    GridView1.Rows[i].Cells[8].ForeColor = System.Drawing.Color.Red;
            }
        }
        else 
        {
            GridView1.DataSource = Ptest.GetPIMData();
            GridView1.DataBind();
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if (GridView1.Rows[i].Cells[8].Text == "Fail")
                    GridView1.Rows[i].Cells[8].ForeColor = System.Drawing.Color.Red;
            }
        
        }
    }

    protected void butSelect_Click(object sender, EventArgs e)
    {
        BindTests();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindTests();
    }
   
  
    private void ExportDataGrid(string FileType, string FileName)
    {
        //导出全部数据，取消分页
        GridView1.AllowPaging = false;
        GridView1.ShowFooter = false;
        BindTests();
        //定义文档、字符编码 
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "UTF-8"; 
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        GridView1.EnableViewState = false;
        Response.ContentEncoding = Encoding.UTF8;
        Response.ContentType = FileType;
        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);

        //定义一个输入流　　   
        StringWriter tw = new StringWriter(myCItrad);
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        ClearControls(GridView1);
        //目标数据绑定到输入流输出　  
        GridView1.RenderControl(hw);

        Response.Write(tw.ToString());
        Response.End();
        //还原分页显示
        GridView1.AllowPaging = true;
        GridView1.ShowFooter = true;
        BindTests();
    }

    protected void ImPort_Click(object sender, EventArgs e)
    {
        ExportDataGrid("application/ms-excel", "大容量交调数据表.xls");

    }


    /// <summary>
    /// 清除控件中的所有控件，以便导出Excel
    /// </summary>
    /// <param name="control"></param>
    private void ClearControls(Control control)
    {
        for (int i = control.Controls.Count - 1; i >= 0; i--)
        {
            ClearControls(control.Controls[i]);
        }

        if (!(control is TableCell))
        {
            if (control.GetType().GetProperty("SelectedItem") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                try
                {
                    literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
                }
                catch
                {
                }
                control.Parent.Controls.Remove(control);
            }
            else if (control.GetType().GetProperty("Text") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                control.Parent.Controls.Remove(control);
            }
        }
        return;
    }
}
